package com.itsitio.registeradmin.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itsitio.registeradmin.dao.BranchDao;
import com.itsitio.registeradmin.dao.CompanyDepartmentDao;
import com.itsitio.registeradmin.dao.CompanyPositionDao;
import com.itsitio.registeradmin.dao.NewsDao;
import com.itsitio.registeradmin.dao.PortalDao;
import com.itsitio.registeradmin.dao.UserDao;
import com.itsitio.registeradmin.model.Branch;
import com.itsitio.registeradmin.model.CompanyDepartment;
import com.itsitio.registeradmin.model.CompanyPosition;
import com.itsitio.registeradmin.model.MilesDetail;
import com.itsitio.registeradmin.model.News;
import com.itsitio.registeradmin.model.Portal;
import com.itsitio.registeradmin.model.User;
import com.itsitio.registeradmin.service.UserSrv;

@Service
@Transactional
public class UserSrvImpl implements UserSrv {

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private CompanyDepartmentDao departmentDao;
	
	@Autowired
	private CompanyPositionDao positionDao;
	
	@Autowired
	private BranchDao branchDao;
	
	@Autowired
	private PortalDao portalDao;
	
	@Autowired
	private NewsDao newsDao;
	
	
	/* (non-Javadoc)
	 * @see com.itsitio.registeradmin.service.UserSrv#findAll()
	 */
	public List<User> findAll() {
		return userDao.obtenerTodos();
	}

	/* (non-Javadoc)
	 * @see com.itsitio.registeradmin.service.UserSrv#get(java.lang.Long)
	 */
	public User get(Integer idUsuario) {
		//TODO borrar este metodo
		return userDao.obtener(idUsuario);
	}

	/* (non-Javadoc)
	 * @see com.itsitio.registeradmin.service.UserSrv#getUserListByBranchId(java.lang.Integer)
	 */
	public List<User> getUserListByBranchId(Integer branchId) {
		System.out.println("===== UserSrvImpl.getUserListByBranchId(branchId="+branchId+")");
		
		List<User> result = userDao.getUserListByBranch(branchId);

		if (result !=null)
			System.out.println("===== UserSrvImpl.getUserListByBranchId() result.size()="+result.size());
		else
			System.out.println("===== UserSrvImpl.getUserListByBranchId() result is null");
		
		return result;
	}

	/* (non-Javadoc)
	 * @see com.itsitio.registeradmin.service.UserSrv#findUserByFilter(String, String);	
	 */
	public List<User> findUserByFilter(String lastName, String email){
		System.out.println("===== UserSrvImpl.findUserByFilter(lastName="+lastName+", email="+email+")");

		List<User> result = userDao.findUserByFilter(lastName, email);
		
		if (result !=null)
			System.out.println("===== UserSrvImpl.findUserByFilter() result.size()="+result.size());
		else
			System.out.println("===== UserSrvImpl.findUserByFilter() result is null");
		
		return result;		
	}
	
	@Override
	public User updateUserWithUserRegistrationData(Integer selectedUser,Integer branchId,
			String name, String lastName, String email,
			Integer userCompanyPosition, Integer userCompanyDepartment, String phone) {
		
		User user = userDao.obtener(selectedUser);
		
		CompanyDepartment department = departmentDao.obtener(userCompanyDepartment);
		
		CompanyPosition position = positionDao.obtener(userCompanyPosition);
		
		Branch branch = branchDao.obtener(branchId);
		
		user.setCompanyDepartment(department);
		user.setCompanyPosition(position);
		user.setEmail(email);
		user.setFirstName(name);
		user.setLastName(lastName);
		user.setBranch(branch);
		user.setPhone(phone);
		
		userDao.guardar(user);
		return user;
	}

	@Override
	public User createUserFromUserRegistration(Integer branchId,String name, String lastName,
			String email, Integer userCompanyPosition,
			Integer userCompanyDepartment, String phone) {
		
		CompanyDepartment department = departmentDao.obtener(userCompanyDepartment);
		
		CompanyPosition position = positionDao.obtener(userCompanyPosition);
		
		Branch branch = branchDao.obtener(branchId);
		
		User user = new User();
		
		user.setCompanyDepartment(department);
		user.setCompanyPosition(position);
		user.setEmail(email);
		user.setFirstName(name);
		user.setLastName(lastName);
		user.setBranch(branch);
		user.setPhone(phone);
		
		userDao.guardar(user);
		
		return user;
	}

	@Override
	@Transactional
	public User obtener(Integer userId) {
		User user = userDao.obtener(userId);
		return user;
	}

	@Override
	public void updateUserPortals(Integer[] selectedPortals, User user) {
		List<Portal> portales = portalDao.obtenerGrupo("id", Arrays.asList(selectedPortals));
		user.setPortales(userDao.getUserPortals(user));
		user.updatePortals(portales);
		userDao.guardar(user);
	}

	@Override
	public void updateUserNews(Integer[] selectedNews, User user) {
		List<News> newsList = newsDao.obtenerGrupo("id", Arrays.asList(selectedNews));
		user.setNews(userDao.getUserNews(user));
		user.updateNews(newsList);
		userDao.guardar(user);
	}

	@Override
	public List<Portal> getUserPortals(User user) {
		return userDao.getUserPortals(user);
	}

	@Override
	public List<News> getUserNews(User user) {
		return userDao.getUserNews(user);
	}

	@Override
	public List<MilesDetail> getUserMilesDetail(User user) {
		return userDao.getUserMilesDetail(user);
	}

	
}
